package com.example.newhabitclock.HabitAlarm

import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.core.app.NotificationCompat
import com.example.newhabitclock.R

class HabitAlarmReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        val habitId = intent.getIntExtra("habit_id", -1)
        val habitName = intent.getStringExtra("habit_name") ?: "Habit"

        val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        val channelId = "habit_reminder_channel"

        // 创建通知渠道（Android 8.0+）
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(
                channelId,
                "Habit Reminders",
                NotificationManager.IMPORTANCE_HIGH
            ).apply {
                description = "Channel for habit reminders"
            }
            notificationManager.createNotificationChannel(channel)
        }

        // 构建通知
        val notification = NotificationCompat.Builder(context, channelId)
            .setSmallIcon(R.drawable.aaa) // 替换为你的图标
            .setContentTitle("习惯提醒")
            .setContentText("到时间执行你的习惯啦：$habitName")
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setAutoCancel(true)
            .build()

        // 显示通知
        notificationManager.notify(habitId, notification)
    }
}